package cn.iocoder.yudao.framework.operate.core.media.impl;

import cn.iocoder.yudao.framework.operate.core.enums.MediaEnum;
import cn.iocoder.yudao.framework.operate.core.media.MediaClient;
import cn.iocoder.yudao.framework.operate.core.media.MediaClientFactory;
import cn.iocoder.yudao.framework.operate.core.media.impl.ocean.OceanClient;
import cn.iocoder.yudao.framework.operate.core.property.MediaProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;

import java.util.Arrays;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * 短信客户端工厂接口
 *
 * @author zzf
 */
@Validated
@Slf4j
public class MediaClientFactoryImpl implements MediaClientFactory {

    /**
     * 电商平台 Map
     * id：渠道编号，使用 {@link MediaProperties#getId()}
     */
    private final ConcurrentMap<Integer, AbstractMediaClient> channelIdClients = new ConcurrentHashMap<>();

    /**
     * 电商平台 Map
     * uniCode：渠道编码，使用 {@link MediaProperties#getChannelCode()} ()} ()}
     */
    private final ConcurrentMap<String, AbstractMediaClient> channelCodeClients = new ConcurrentHashMap<>();

    public MediaClientFactoryImpl() {
        // 初始化 channelCodeClients 集合
        Arrays.stream(MediaEnum.values()).forEach(channel -> {
            // 创建一个空的 GoodsPlatformProperties 对象
            MediaProperties properties = new MediaProperties().setChannelCode(channel.getCode())
                    .setAppId("default default").setAppSecret("default").setAppKey("default").setAccessNotifyUrl("default");
            // 创建  客户端
            AbstractMediaClient client = createMediaClient(properties);
            channelCodeClients.put(channel.getCode(), client);
        });
    }

    @Override
    public MediaClient getMediaClient(Integer mediaId) {
        return channelIdClients.get(mediaId);
    }

    @Override
    public MediaClient getMediaClient(String uniCode) {
        return channelCodeClients.get(uniCode);
    }

    @Override
    public void createOrUpdateMediaClient(MediaProperties properties) {
        AbstractMediaClient client = channelIdClients.get(properties.getId());
        if (client == null) {
            client = this.createMediaClient(properties);
            client.init();
            channelIdClients.put(client.getId(), client);
        } else {
            client.refresh(properties);
        }
    }

    private AbstractMediaClient createMediaClient(MediaProperties properties) {
        MediaEnum channelEnum = MediaEnum.getByCode(properties.getChannelCode());
        Assert.notNull(channelEnum, String.format("渠道类型(%s) 为空", channelEnum));
        // 创建客户端
        switch (channelEnum) {
            case DY:
            case KS: return new OceanClient(properties);
        }
        // 创建失败，错误日志 + 抛出异常
        log.error("[createMediaClient][配置({}) 找不到合适的客户端实现]", properties);
        throw new IllegalArgumentException(String.format("配置(%s) 找不到合适的客户端实现", properties));
    }


}
